package com.oa.log.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.oa.log.dao.LogDao;
import com.oa.log.dto.LogEntityDto;
import com.oa.log.entity.LogEntity;
import com.oa.system.dao.UserInfoDao;
import com.oa.system.entity.UserInfo;

@Service
public class LogManagerServiceImpl implements LogManagerService {
	@Resource
	private UserInfoDao userinfoDao;
	@Resource
	private LogDao logDao;

	private String[] excelHeader = { "操作人", "操作时间", "具体操作", "IP地址", "结果" };
	private SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

	@Override
	public Page<LogEntityDto> findAllLog(LogEntityDto logDto, Pageable pageable) {
		String fullname = logDto.getFullname();
		if (fullname != null) {
			UserInfo info = userinfoDao.findByFullname(fullname);

			if (info != null) {
				logDto.setUserId(info.getId());
			}
		}

		Page<LogEntity> logPage = logDao.findAll(LogEntityDto.getLogManagerQuery(logDto), pageable);

		Page<LogEntityDto> logDtoPage = logPage.map(new Converter<LogEntity, LogEntityDto>() {
			@Override
			public LogEntityDto convert(LogEntity source) {
				String fullname = userinfoDao.findOne(source.getUserId()).getFullname();
				return LogEntityDto.toLogEntityDto(source, fullname);
			}
		});

		return logDtoPage;
	}

	@Override
	public HSSFWorkbook export() {
		Iterable<LogEntity> logList = logDao.findAll();
		List<LogEntityDto> logDtoList = new ArrayList<>();

		for (LogEntity logEntity : logList) {
			String fullname = userinfoDao.findOne(logEntity.getUserId()).getFullname();
			LogEntityDto logEntityDto = LogEntityDto.toLogEntityDto(logEntity, fullname);

			logDtoList.add(logEntityDto);
		}

		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("日志");
		HSSFRow row = sheet.createRow((int) 0);

		HSSFCellStyle style = wb.createCellStyle();
		style.setBorderBottom(BorderStyle.THIN);
		style.setBorderLeft(BorderStyle.THIN);
		style.setBorderRight(BorderStyle.THIN);
		style.setBorderTop(BorderStyle.THIN);
		style.setAlignment(HorizontalAlignment.CENTER);

		Font font = wb.createFont();
		font.setBold(true);
		font.setFontHeightInPoints((short) 13);
		style.setFont(font);

		for (int i = 0; i < excelHeader.length; i++) {
			HSSFCell cell = row.createCell(i);
			cell.setCellValue(excelHeader[i]);
			cell.setCellStyle(style);
			sheet.autoSizeColumn(i);
		}

		HSSFCellStyle rowstyle = wb.createCellStyle();
		rowstyle.setBorderBottom(BorderStyle.THIN);
		rowstyle.setBorderLeft(BorderStyle.THIN);
		rowstyle.setBorderRight(BorderStyle.THIN);
		rowstyle.setBorderTop(BorderStyle.THIN);

		Font rowfont = wb.createFont();
		rowfont.setBold(false);
		rowfont.setFontHeightInPoints((short) 11);
		rowstyle.setFont(rowfont);

		for (int i = 0; i < logDtoList.size(); i++) {
			row = sheet.createRow(i + 1);
			LogEntityDto log = logDtoList.get(i);

			row.createCell(0).setCellValue(log.getFullname());
			row.getCell(0).setCellStyle(rowstyle);
			row.createCell(1).setCellValue(format.format(log.getOprateTime()));
			row.getCell(1).setCellStyle(rowstyle);
			row.createCell(2).setCellValue(log.getOprate());
			row.getCell(2).setCellStyle(rowstyle);
			row.createCell(3).setCellValue(log.getIp());
			row.getCell(3).setCellStyle(rowstyle);

			if (log.isSuccess()) {
				row.createCell(4).setCellValue("成功");
			} else {
				row.createCell(4).setCellValue("失败");
			}
			row.getCell(4).setCellStyle(rowstyle);

			sheet.autoSizeColumn(i);
		}

		return wb;
	}
}
